package src.com.susiqi.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import src.com.susiqi.dto.LoginDto;
import src.com.susiqi.dto.UserDto;
import src.com.susiqi.dto.UserPageDto;
import src.com.susiqi.result.Result;
import src.com.susiqi.service.UserService;

@RestController
@RequestMapping("/admin/users")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 管理员登录
     * @param loginDto 登录表单
     * @param httpServerRequest
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto, HttpServletRequest httpServerRequest){
        return userService.login(loginDto,httpServerRequest);
    }

    /**
     * 根据用户id查询用户信息
     * @param id 用户id
     * @return
     */
    @GetMapping("/getById")
    public Result getById(@RequestParam("id") Long id){
        return userService.getById(id);
    }

    /**
     * 新增或修改用户
     * @param userDto 用户信息
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody UserDto userDto){
        return userService.saveOrUpdate(userDto);
    }

    /**
     * 根据用户id删除用户
     * @param id 用户id
     * @return
     */
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam("id") Long id){
        return userService.deleteById(id);
    }

    /**
     * 分页查询用户
     * @param userPageDto
     * @return
     */
    @GetMapping("/getPage")
    public Result getPage(UserPageDto userPageDto){
        return userService.getPage(userPageDto);
    }
}
